This solution is generated by ChatGPT, correctness is not guaranteed.

AI solution for JavaScript Coding Question on BFE.dev
99. extract all anchor element from HTML string

One possible solution to this problem is to use the DOMParser API to convert the HTML string into a DOM document, and then use the querySelectorAll() method to find all anchor elements in the document. Here's an implementation:

function extract(html) {
  const parser = new DOMParser();
  const doc = parser.parseFromString(html, 'text/html');
  const anchors = doc.querySelectorAll('a');
  return Array.from(anchors).map(a => a.outerHTML);
}

We start by creating a new DOMParser object and calling its parseFromString() method with the HTML string and the MIME type 'text/html'. This returns a new Document object that represents the parsed HTML.

Next, we use the querySelectorAll() method on the document to find all a elements. This returns a NodeList object, which we can convert into an array using the Array.from() method. Finally, we use the map() method to extract the outerHTML property of each anchor element, which gives us the complete HTML code for the anchor tag.

Note that this solution assumes that the input HTML string is well-formed and contains no syntax errors. If the input HTML is invalid, the parseFromString() method may throw an error.